package com.codejiwei.flink.practice;

import com.codejiwei.flink.entity.WaterSensor;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;

/**
 * @author jiwei
 * @description 分组聚合后, 理论上只能取分组字段和聚合结果, 但是Flink允许其他的字段也可以取出来,
 * 其他字段默认情况是取的是这个组内第一个元素的字段值
 * @date 2023/5/22 11:14
 */
public class Flink_Transform_Aggregate2 {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 50));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 30));

        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));

        env.fromCollection(waterSensors)
                .keyBy(WaterSensor::getId)
                .sum("vc")
                .print("group sum...");

        env.execute();
    }
}
